package com.chinawlx.wlxteacher.ui.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.base.WLXApplication;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.bean.WLXLogOffBean;
import com.chinawlx.wlxteacher.utils.WLXActivityManagerUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXFileUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXSystemUtil;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;
import com.chinawlx.wlxteacher.wlx_auth;
import com.chinawlx.wlxteacher.wlx_authDao;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by wlx on 16/8/30.
 */
public class WLXSettingActivity extends WLXBaseActivity {
    @BindView(R.id.iv_back_setting)
    ImageView backBtn;
    @BindView(R.id.rl_about_setting)
    RelativeLayout aboutWLX;
    @BindView(R.id.rl_score_setting)
    RelativeLayout score;
    @BindView(R.id.btn_logout_setting)
    Button logoutBtn;
    @BindView(R.id.tv_cache_setting)
    TextView mTvCacheSetting;
    @BindView(R.id.tv_version_setting)
    TextView mTvVersionSetting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initDate();
        initView();
    }

    private void initDate() {

    }

    private void initView() {
        mTvCacheSetting.setText(WLXFileUtil.getCacheFileSize(WLXSettingActivity.this));
        mTvVersionSetting.setText(WLXSystemUtil.getAppVersionName(WLXApplication.myApplication));
    }

    @OnClick({R.id.iv_back_setting, R.id.rl_about_setting, R.id.rl_score_setting, R.id
            .btn_logout_setting, R.id.rl_setting_clear})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_setting:
                finish();
                break;
            case R.id.rl_about_setting:     //关于未来星
                startActivity(WLXAboutFutrueStarActivity.class);
                break;
            case R.id.rl_score_setting:    //去评分
                try {
                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    WLXCustomToast.show("无法启动应用市场");
                }
                break;
            case R.id.rl_setting_clear:

                new AlertView("清除缓存", null, "取消", null, new String[]{"确定"}, this, AlertView.Style
                        .ActionSheet, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 0) {
                            WLXFileUtil.deleteCache();
                            mTvCacheSetting.setText(WLXFileUtil.getCacheFileSize
                                    (WLXSettingActivity.this));
                        }
                    }
                }).show();
                break;
            case R.id.btn_logout_setting:   //注销
                WLXHttpRxHelper.getInstance().logoff(new Subscriber<WLXLogOffBean>() {
                    @Override
                    public void onCompleted() {
                        //注销成功,修改数据库
                        wlx_auth auth = WLXGreenDaoUtil.getAuthDao().queryBuilder().where
                                (wlx_authDao.Properties.User_id.eq(WLXConstant.USER_ID)).list()
                                .get(0);
                        WLXConstant.IS_ACTIVE = 0;
                        WLXConstant.USER_ID = "0";

                        auth.setIs_active(WLXConstant.IS_ACTIVE);
                        WLXGreenDaoUtil.getAuthDao().update(auth);

                        WLXActivityManagerUtil.finishActivity(WLXCourseOrClazzActivity.class);
                        //跳转到登录界面
                        startActivity(WLXLoginActivity.class, true);
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        onCompleted();
                    }

                    @Override
                    public void onNext(WLXLogOffBean logoffBean) {

                    }
                });
                break;
        }
    }
}
